<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    ***/
    class ajaxAddTag extends ajaxAction{

        public function process(){
            $pageid = (int)$_REQUEST['page'];
            
            if($_REQUEST['tagid'] == 'new'){
                $tag    = $_REQUEST['tname'];
                
                //make sure there is no empty tag mon
                if(empty($tag)){
                    $tag = 'n/a';
                }
                
                $tag = $this->unescape($tag);
                
                //transliterate tag
                $myTranslit = new translitWrap();
                $name       = $myTranslit->UrlTranslit($tag);
                
                //is there THIS tag in the DB already?
                if(!$tagID = $this->db->getOne("SELECT id FROM tags WHERE url = ".$this->db->qstr($name))){
                    $this->db->Execute("INSERT INTO tags(name, url) VALUES(".$this->db->qstr($tag).",".$this->db->qstr($name).")");
                    $tagID = $this->db->getOne("select last_value from tags_id_seq");
                }
            }else{
                $tagID = $_REQUEST['tagid'];
            }
            
            //create a map reference (if needed)
            if(!$this->db->getOne("SELECT page_id FROM tag_page_map WHERE page_id = ".(int)$pageid." AND tag_id = ".(int)$tagID)){
                $this->db->Execute("INSERT INTO tag_page_map(tag_id, page_id) VALUES(".(int)$tagID.",".(int)$pageid.")");
                $this->r->added = true;
            }
                        
            //return the new tag id
            $this->r->tagid  = $tagID;
            $this->r->tagurl = $name;
        }
    }
?>